#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@author zyx
@since 2022/2/10 23:44
@file: c02_cookie应用.py
"""

import requests
import os
import jsonpath

# https://xueqiu.com/ 爬取热帖内容
# 通过 Fetch/XHR 获取Ajax内容
# https://xueqiu.com/statuses/hot/listV2.json?since_id=-1&max_id=311519&size=15

import requests

# 1.创建一个空白的session对象
session = requests.Session()

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/98.0.4758.80 Safari/537.36',
}
main_url = 'https://xueqiu.com/'
# 2.使用session发起的请求，目的是为了捕获到cookie，且将其存储到session对象中
session.get(url=main_url, headers=headers)

url = 'https://xueqiu.com/statuses/hot/listV2.json'
param = {
    "since_id": "-1",
    "max_id": "311519",
    "size": "15",
}
# 3.就是使用携带了cookie的session对象发起的请求（就是携带者cookie发起的请求）
response = session.get(url=url, headers=headers, params=param)
data = response.json()
for desc in jsonpath.jsonpath(data, '$..text'):
    print("==> ", desc)
